//
//  SharkExampleVC.m
//  YiBaoStore
//
//  Created by 毛云河 on 2018/10/5.
//  Copyright © 2018年 小河. All rights reserved.
//

#import "SharkExampleVC.h"

@interface SharkExampleVC ()

@property (nonatomic, strong) UIScrollView *myScrollView;

@property (nonatomic, strong) UIImageView *imageTextImage;
@property (nonatomic, strong) UIImageView *moreSharkImage;
@property (nonatomic, strong) UIImageView *shoppingSharkImage;

@property (nonatomic, strong) UIButton *imageTextBtn;
@property (nonatomic, strong) UIButton *moreSharkBtn;
@property (nonatomic, strong) UIButton *shoppingSharkBtn;

@property (nonatomic, assign) NSInteger index;

@end

@implementation SharkExampleVC

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self setNav];
    [self createDownBtn];// 1
    [self createScrollView];// 2
    
    //初始化索引
    self.index = 0;
}

- (void)setNav
{
    self.titLabel.text = @"图文分享示例";
}

#pragma mark - 创建下部单选按钮
- (void)createDownBtn
{
    //1，下部按钮
    NSInteger count = self.templatesArray.count;
    CGFloat jiange = (SCREENWIDTH-(60*count))/(count+1);
    
    for (int i = 0; i < count; i++)
    {
        UIButton * button = [UIButton buttonWithType:UIButtonTypeCustom];
        [self.view addSubview:button];
        if (@available(iOS 11.0, *))
        {
            button.frame  = CGRectMake(jiange+(60+jiange)*(i%count), (SCREENHEIGHT-60-102-BottomHeight_xs)+(60+0)*(i/count), 60, 60);
        } else {
            // Fallback on earlier versions
            button.frame  = CGRectMake(jiange+(60+jiange)*(i%count), (SCREENHEIGHT-60-102-BottomHeight)+(60+0)*(i/count), 60, 60);
        }
        button.tag = i+1;
        
         TemplatesModel *model = self.templatesArray[i];
        //设置图片
        [button sd_setImageWithURL:[NSURL URLWithString:model.icon] forState:UIControlStateNormal];
        [button sd_setImageWithURL:[NSURL URLWithString:model.selectIcon] forState:UIControlStateSelected];
        
        //设置文字
        [button setTitle:model.title forState:UIControlStateNormal];
        button.titleLabel.textAlignment = 1;
        button.titleLabel.font = [UIFont systemFontOfSize:14];
        [button setTitleColor:RGBACOLOR(154, 154, 153, 1) forState:UIControlStateNormal];
        [button setTitleColor:BlackTextColor forState:UIControlStateSelected];
        
        //设置初始化选中项
        if (i == 0)
        {
            button.selected = YES;
        }
        
        // 获取button的宽高
        button.imageView.bounds = CGRectMake(0, 0, 30, 30);
        button.imageView.contentMode = UIViewContentModeScaleAspectFit;
        button.imageView.clipsToBounds = YES;
        CGFloat buttonHeight = CGRectGetHeight(button.bounds);
        CGFloat buttonWidth  = CGRectGetWidth(button.bounds);
        CGFloat buttonImageWidth  = CGRectGetWidth(button.imageView.bounds);
        UIEdgeInsets edge = UIEdgeInsetsMake(0, (buttonWidth-buttonImageWidth)/2, 30, (buttonWidth-buttonImageWidth)/2);
        button.imageEdgeInsets = edge;
        button.titleEdgeInsets = UIEdgeInsetsMake(buttonHeight-edge.bottom+10, -buttonWidth/2, 5, 0);
        
        // 添加绑定事件
        [button addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
    }
}

#pragma mark - 设置滚动卡片
- (void)createScrollView
{
    UIButton *button = [self.view viewWithTag:1];
    
    //2,滚动视图和方向按钮
    CGFloat hw = 369/225.f;
    CGFloat width = SCREENWIDTH-52-50-48;
    
    self.myScrollView = [[UIScrollView alloc] init];
    [self.view addSubview:self.myScrollView];
    if (@available(iOS 11.0, *))
    {
        self.myScrollView.frame = CGRectMake(26+25+24, (button.top-46-(width*hw)), width, width*hw);
    } else {
        // Fallback on earlier versions
        self.myScrollView.frame = CGRectMake(26+25+24, (button.top-46-(width*hw)), width, width*hw);
    }
    self.myScrollView.contentSize = CGSizeMake(width*self.templatesArray.count, 0);
    self.myScrollView.showsHorizontalScrollIndicator = NO;
    self.myScrollView.pagingEnabled = YES;
    self.myScrollView.bounces = NO;
    self.myScrollView.scrollEnabled = NO;
    self.myScrollView.layer.borderWidth = 1;
    self.myScrollView.layer.borderColor = RGBACOLOR(154, 154, 153, 0.2).CGColor;
    
    for (int i = 0; i < self.templatesArray.count; i++)
    {
        TemplatesModel *model = self.templatesArray[i];
        UIImageView *imageView =[[UIImageView alloc] init];
        [self.myScrollView addSubview:imageView];
        [imageView sd_setImageWithURL:[NSURL URLWithString:model.previewImageUrl] placeholderImage:[UIImage imageNamed:@"Placeholder400"]];
        imageView.frame = CGRectMake(self.myScrollView.width*i, 0, self.myScrollView.width, self.myScrollView.height);
    }
    
    //3,两侧的按钮
    for (int i = 0; i < 2; i++)
    {
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        [self.view addSubview:button];
        button.tag = i+1;
        [button addTarget:self action:@selector(leftRightBtnClick:) forControlEvents:UIControlEventTouchUpInside];
        if (i == 0) {
            button.center = CGPointMake(self.myScrollView.centerX-(width/2)-24-13, self.myScrollView.centerY);
            button.bounds = CGRectMake(0, 0, 26, 26);
            [button setBackgroundImage:[UIImage imageNamed:@"left_btn"] forState:UIControlStateNormal];
        }
        else {
            button.center = CGPointMake(self.myScrollView.centerX+(width/2)+24+13, self.myScrollView.centerY);
            button.bounds = CGRectMake(0, 0, 26, 26);
            [button setBackgroundImage:[UIImage imageNamed:@"right_btn"] forState:UIControlStateNormal];
        }
    }
}

#pragma mark - 左右方向键点击事件
- (void)leftRightBtnClick:(UIButton *)sender
{
    if (sender.tag == 1)
    {
        if (self.index>=1)
        {
            NSLog(@"向左");
            self.index--;
        }
    }
    else
    {
        if (self.index < self.templatesArray.count-1)
        {
            NSLog(@"向右");
            self.index++;
        }
    }
    [self changeScrollViewAndBtnStuWith:self.index];
}

#pragma mark - 图文，多图文，商品分享按钮点击事件
- (void)buttonClick:(UIButton *)sender
{
    self.index = sender.tag-1;
    [self changeScrollViewAndBtnStuWith:self.index];
}

- (void)changeScrollViewAndBtnStuWith:(NSInteger)index
{
    NSLog(@"index--%ld",self.index);
    //1，先置NO
    for (int i = 0; i <self.templatesArray.count; i++)
    {
        UIButton *button = [self.view viewWithTag:i+1];
        button.selected = NO;
    }
    
    //2，在设置选中项
    UIButton *button = [self.view viewWithTag:index+1];
    button.selected = YES;
    
    //3，在滑动到相对应的卡片
    [self.myScrollView setContentOffset:CGPointMake(self.myScrollView.width*index, 0) animated:YES];
}


@end
